package Day012_多线程2.ZuoYe;

import java.util.concurrent.Semaphore;

/**
 * @author zhangyan
 * @date 2020/4/30 18:04
 */
public class Test3_6 {
    public static void main(String[] args) {

        ZhanLanShi zls =new ZhanLanShi();
        //创建10条线程,执行任务
        MyRunnable5 mr5 = new MyRunnable5(zls);

        new Thread(mr5,"游客1").start();
        new Thread(mr5,"游客2").start();
        new Thread(mr5,"游客3").start();
        new Thread(mr5,"游客4").start();
        new Thread(mr5,"游客5").start();
        new Thread(mr5,"游客6").start();
        new Thread(mr5,"游客7").start();
        new Thread(mr5,"游客8").start();
        new Thread(mr5,"游客9").start();
        new Thread(mr5,"游客10").start();
    }
}

class ZhanLanShi {
    //创建许可对象
    Semaphore sp = new Semaphore(3);

    public void into(){
        //获取许可证才能进
        try {
            sp.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "参观展览室");
        try {
            Thread.sleep(2000); //停留2秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //出去了,释放许可
        System.out.println(Thread.currentThread().getName() + "走出展览室");
        sp.release();
    }
}

class MyRunnable5 implements Runnable{

    ZhanLanShi zls;
    public MyRunnable5(ZhanLanShi zls){
        this.zls = zls;
    }

    @Override
    public void run() {
        zls.into();
    }
}
